#!/bin/bash

CONFIG=$(
    cat <<-EOF
     --disable-stripping
     --cpu=generic
     --target-os=none
     --arch=x86_64
     --disable-doc
     --disable-parsers
     --disable-bsfs
     --disable-debug
     --disable-ffmpeg
     --disable-ffplay
     --disable-ffprobe
     --disable-symver
     --disable-devices
     --disable-pthreads
     --disable-w32threads
     --disable-network
     --disable-everything
     --disable-asm
     --disable-sdl2
     --disable-libvpx
     --enable-gpl
     --enable-version3
     --enable-nonfree
     --enable-cross-compile
     --enable-parser=aac
     --enable-demuxer=aac
     --enable-decoder=aac
     --enable-parser=h264
     --enable-demuxer=h264
     --enable-decoder=h264
EOF
)

CURDIR=$(pwd)
FFMPEG=ffmpeg
PREFIX=/usr/local/etc

# echo "准备ffmpeg源文件"
# git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
# > git clone -b release/5.0 --depth=1 https://git.ffmpeg.org/ffmpeg.git ffmpeg

sudo apt install llvm,cmake

echo "安装目录: "${PREFIX}
sudo chmod 757 ${PREFIX}

echo "编译普通版本"
rm ${FFMPEG}/build -rf
mkdir -p ${FFMPEG}/build
cd ${FFMPEG}/build

# -----------------------------------------------------------------------------------------------------------
echo "配置..."
../configure ${CONFIG} --disable-optimizations --enable-debug --enable-ffmpeg --prefix=${PREFIX}/ffmpeg/
echo "编译..."
make -j
echo "安装..."
make install

cd ${CURDIR}

# -----------------------------------------------------------------------------------------------------------
echo "编译emscripten版本"
rm ${FFMPEG}/build_em -rf
mkdir -p ${FFMPEG}/build_em
cd ${FFMPEG}/build_em

echo "配置..."
emconfigure ../configure ${CONFIG} --cc="emcc" --cxx="em++" --ar="emar" --ranlib="emranlib" --nm="llvm-nm" --prefix=${PREFIX}/ffmpeg_em/
echo "编译..."
emmake make -j
echo "安装..."
make install

cd $CURDIR

echo ""
echo "--------------------------------------------"
echo "安装完成..."
echo "普通版ffmpeg库位置:       "${PREFIX}/ffmpeg/
echo "emscripten版ffmpeg库位置: "${PREFIX}/ffmpeg_em/
